package com.itheima.tlias84.aspect;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.utils.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Enumeration;

/**
 * 统计接口耗时切面逻辑
 */
@Aspect // 这个类就变成了切面类
//@Component
@Slf4j
public class CountTimeAspect {
    @Resource
    HttpServletRequest request;
    @Around("execution(* com.itheima.tlias84.servrce.impl.*.*(..))")
  public Object count(ProceedingJoinPoint point){
        //获取token 来解析 操作人的姓名
        String token= request.getHeader("token");
        //这里的判断是因为 登录界面  进行判断 否则登录一直失败
        if (token!=null)
        {
            JSONObject jsonObject = JwtUtil.verifyToken(token);
            log.info("获取token{}",token);
        }
        //获取参数
        Object[] args = point.getArgs();
        log.info("请求参数{}",args);
        //获取类型
        Object target = point.getTarget();
        log.info("获取类名对象{}",target);

        long l = System.currentTimeMillis();
      //执行父类方法
        Object proceed = null;
        try {
            proceed = point.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        long end= System.currentTimeMillis() - l;
     log.info("{}该接口执行时间为{}",point.getSignature().getName(),end);
     return proceed;
  }
}